#!/usr/bin/env python
#
# Copyright (C) 2011-2020 ABINIT Group (Yann Pouillon)
#
# This file is part of the ABINIT software document. For license information,
# please see the COPYING file in the top-level directory of the ABINIT source
# distribution.
#
from __future__ import print_function, division, absolute_import #, unicode_literals

try:
    from ConfigParser import ConfigParser,NoOptionError
except ImportError:
    from configparser import ConfigParser,NoOptionError
from time import gmtime,strftime

import os
import re
import sys

class MyConfigParser(ConfigParser):

  def optionxform(self,option):
    return str(option)

# ---------------------------------------------------------------------------- #

#
# Subroutines
#

# Makefile contents
def makefile_data(name,stamp,files):

  return """\
#                                                          -*- Automake -*-
# Makefile for the Abinit Documentation
# Generated by %s on %s

#
# IMPORTANT NOTE
#
# Any manual change to this file will systematically be overwritten.
# Please modify the %s script or its config file instead.
#

nobase_dist_doc_DATA =%s

EXTRA_DIST = README built-docs.tgz

CLEANFILES = doc-build-stamp

all-local: doc-build-stamp

doc-build-stamp:
	touch doc-build-stamp
""" % (name,stamp,name," \\\n  " + " \\\n  ".join(files))



# ---------------------------------------------------------------------------- #

#
# Main program
#

# Initial setup
my_name    = "make-makefiles-doc"
my_configs = ["config/specs/documents.conf"]

# Check if we are in the top of the ABINIT source tree
if ( not os.path.exists("configure.ac") or
     not os.path.exists("config/specs/documents.conf") ):
  print("%s: You must be in the top of an Abinit Documentation source tree." % my_name)
  print("%s: Aborting now." % my_name)
  sys.exit(1)

# Read config file(s)
for cnf_file in my_configs:
  if ( os.path.exists(cnf_file) ):
    if ( re.search("\.cf$",cnf_file) ):
      exec(compile(open(cnf_file).read(), cnf_file, 'exec'))
  else:
    print("%s: Could not find config file (%s)." % (my_name,cnf_file))
    print("%s: Aborting now." % my_name)
    sys.exit(2)

# What time is it?
now = strftime("%Y/%m/%d %H:%M:%S +0000",gmtime())

# Prepare regexps
olddir = re.compile(",.*")
tmpdir = re.compile("tmp-*")
vimswp = re.compile("\..*\.swp")

# Init document list
cnf = MyConfigParser()
cnf.read(my_configs[0])
doc_list = cnf.sections()
doc_list.sort()
dat_files = list()
ign_list = ["Makefile.am","Makefile.in","Makefile"]


# Process each document
for doc_dir in doc_list:

    for root,dirs,files in os.walk(doc_dir):
      # We don't want to distribute nor install temporary directories
      dirs = [item for item in dirs if (not olddir.match(item)) and (not tmpdir.match(item))]

      # Get rid of ignored files
      files = [item for item in files if (not item in ign_list) and (not vimswp.match(item))]
      files.sort()

      # Just add files as raw data
      dat_files += [os.path.join(root,item) for item in files]

# Write makefile
mf = open("./Makefile.am", "wt")
mf.write(makefile_data(my_name,now,dat_files))
mf.close()
